<template>
    <main-wrap :loading="true" ref="mianEle" :load="onLoad" :end="page.finished" :showOverTxt="page.lists.length>0" :pageIndex="page.pageIndex">
        <template v-slot:header>
            <div class="adheader">
                <div class="hcon">
                    <em><span v-t="'public.total'">TOTAL</span>{{allObj.allAmount}}</em>
                    <!-- <div class="zhtxt">comprehensive</div>
                    <div class="sort">
                        <sort :lists="[{id:1,name:'Flow'}]"/>
                    </div> -->
                    <div class="topSearch">
                        <div class="tswd">
                            <my-search v-model="keyword" :placeholder="Search" @onSearch="resetScroll"/>
                        </div>
                    </div>
                </div>
            </div>
        </template>
        <div class="main-con">
            <div class="tabSearchBox">
                <div class="item" :class="{'selected':!placeStatus}" @click="getTypeData('')"><span v-t="'public.all'">All</span>({{allObj.allAmount}})</div>
                <div class="item" :class="{'selected':placeStatus==2}" @click="getTypeData(2)"><span v-t="'public.placed'">Placed</span>({{allObj.haveLaunchAmount}})</div>
                <div class="item" :class="{'selected':placeStatus==1}" @click="getTypeData(1)"><span v-t="'public.noplaced'">Not Placed</span>({{allObj.notLaunchAmount}})</div>
                 <div class="ads-sort">
                        <sort :lists="[{id:'total_pv',name:pvTitle}]" @change="getSortData"/>
                    </div>
            </div>
            <goods-list :lists="page.lists" :type="2" @onClick="openDetail"/>
            <empty v-if="page.finished && page.lists.length<1"/>
        </div>
        <my-dailog v-model:show="showFlag" :showtopclose="true" :styleType="5">
            <detail :detail='details' :id="details.adPlaceId" :type='1'/>
        </my-dailog>
    </main-wrap>
</template>

<script>
import {reactive,toRefs, onMounted,computed,getCurrentInstance } from 'vue'
import { useStore } from 'vuex'
import {useRoute,useRouter} from 'vue-router'
import mainWrap from '@/components/mainWrap.vue'
import {toast} from '@/utils/common.js'
import sort from '@/components/sort.vue';
import mySearch from '@/components/mySearch.vue'
import Image from '@/components/image.vue';
import goodsList from '@/components/goodsList.vue';
import myDailog from '@/components/myDailog.vue'
import detail from '../components/adDetail/detail.vue'
import empty from '@/components/empty.vue'
import { useI18n } from 'vue-i18n'

export default {
    components:{
        mainWrap,
        sort,
        mySearch,
        Image,
        goodsList,
        myDailog,
        detail,
        empty
    },
    setup(){
        const store = useStore()
        const {proxy} = getCurrentInstance()
        const router = useRouter()
        const route = useRoute()
        const {t} = useI18n()

        const state = reactive({
            keyword:'',
            showFlag:false,
            userInfo:computed(()=>store.state.base.userInfo),
            pvTitle:t('public.pv'),
            Search:t('public.search'),
            page:{
                lists:[],
                pageIndex:1,
                pageSize:20,
                finished: false
            },
            sortParams:{
                orderField: 'total_pv',
                orderType: "desc",
            },
            placeStatus:'',
            allObj:{},
            details:'',
            userType:''
        })

        onMounted(()=>{
            state.userType = route.query.userType
            store.dispatch('ad/get_status_total',{walletAddress:state.userType?state.userInfo.walletAddress:''}).then(res=>{
                state.allObj = res.data
            })
            //如果有值从用户进来只查询用户的 或者从广告商进来查所有
            onLoad()
        })

        const onLoad = ()=> {
            // 此函数必须返回一个Promise对象
            return new Promise((resolve, reject) => {
                setTimeout(async () => {
                    const res = await store.dispatch("ad/get_adspace_list", {
                        page: state.page.pageIndex,
                        limit: state.page.pageSize,
                        ...state.sortParams,
                        placeStatus:state.placeStatus,
                        adPlaceTitle:state.keyword,
                         walletAddress:state.userType?state.userInfo.walletAddress:''
                    });
                    state.page.lists = [...state.page.lists, ...res.data];
                    state.page.pageIndex++;
                    if (state.page.lists.length >= res.total) {
                        state.page.finished = true;
                    }
                    resolve(true);
                }, 500);
            });
        }

        const openDetail = item=>{
            console.log(item)
            // state.details = item
            // state.showFlag = true
        } 

        const resetScroll = async()=>{
            proxy.$refs.mianEle.stopScroll()
            state.page.lists=[]
            state.page.pageIndex=1
            state.page.finished=false
            await onLoad()
            proxy.$refs.mianEle.initScroll()
        }

        const getSortData = sortParams =>{
            state.sortParams = sortParams
            resetScroll()
        }
        const getTypeData = status=>{
            state.placeStatus = status
            resetScroll()
        }    

        return {
            ...toRefs(state),
            onLoad,
            resetScroll,
            openDetail,
            getSortData,
            getTypeData
        }
    }
}
</script>

<style lang="scss" scoped>
    .adheader{
         padding: 15px 30px 15px 0;
        .hcon{
            display: flex;
            height: 80px;
            line-height: 80px;
            align-items: center;
           
            em{
                display: block;
                font-size: 28px;
                font-style: normal;
                padding: 10px 0 0 0;
                text-shadow: -1px 0px 0px rgba(0,209,255,0.7000), 1px 0px 0px rgba(255,0,184,0.7000);
                //-webkit-background-clip: text;
                //-webkit-text-fill-color: transparent;
                 span{
                   font-size: 28px;
                   font-style: normal;
             }
            }
            
            .ads-sort{
                padding: 6px 0 0 50px;
            }
            .zhtxt{
                font-size: 30px;
                height: 60px;
                line-height: 60px;
                padding: 0 30px;
                background: url(/images/gloabAdSplace/zh.png) left top no-repeat;
                background-size:100% 100%;
                margin: 0 0 0 30px;
            }
            .sort{
                padding: 0 0 0 30px;
            }
            .topSearch{
                flex: 1;
                // display: flex;
                // justify-content: end;
                .tswd{
                    float:right;
                    width: 550px;
                }
            }
        }
    }
    .main-con{
        padding: 10px 0 0 0;
    }
    .tabSearchBox{
        display: inline-block;
        height: 55px;
        background: url(/images/gloabAdSplace/tsc.png) left top no-repeat;
        background-size: 100% 100%;
        padding: 0 30px 0 0;
        .ads-sort,
        .item{
            display: inline-block;
            padding:0 0 0 30px;
            line-height: 55px;
            font-weight: 500;
            color: rgba(255,255,255,0.5000);
            font-size: 24px;
            &.selected{
                color: #fff;
                font-weight: bold;
            }
        }
    }
    
</style>